﻿using System;
using System.Windows.Forms;
using Finance.Data;
using Finance.Data.Entities;
using Finance.Reports;
using Finance.TodoSoft.UIControls;
using Finance.ViewModels;

namespace Finance.Views
{
	internal partial class FormDayFinance : Form
	{
		private FinanceDataBase _dataBase;
		private DayDocumentsViewModel _dayDocumentsViewModel;

		public DayDocumentsViewModel DayDocumentsViewModel
		{
			get { return _dayDocumentsViewModel; }
			set
			{
				if(_dayDocumentsViewModel == value)
					return;
				
				_dayDocumentsViewModel = value;

				if (_dayDocumentsViewModel != null)
				{
					var dateBinding = new Binding("Value", _dayDocumentsViewModel, "SelectedDate");
					_dateTimePicker.DataBindings.Add(dateBinding);

					_listDocuments.Items = _dayDocumentsViewModel.Items;
				}
			}
		}

		public FormDayFinance()
		{
			InitializeComponent();

			_dateTimePicker.Value = DateTime.Now;
			
			_listDocuments.AddViewType(typeof(Document), typeof (DocumentListItemView));
			_listDocuments.AddViewType(typeof(DebtDocument), typeof(DebtDocumentListItemView));
			_listDocuments.AddViewType(typeof(RepayDebtDocument), typeof(RepayDebtDocumentListItemView));
			_listDocuments.AddViewType(typeof(Note), typeof(NoteListItemView));
		}

		public FinanceDataBase DataBase
		{
			get { return _dataBase; }
			set
			{
				_dataBase = value;

				if (_dataBase != null)
				{

					_debtsReportViewer.DataBase = _dataBase;
					_debtsReportViewer.Report = new DebtsReport((guid, type) =>
						{
							var document =
								type == DocumentType.DohodZaim || type == DocumentType.RashodZaim
									? (IDocumentBase) _dataBase.DebtDocuments.GetItem(guid)
									: _dataBase.RepayDebtDocuments.GetItem(guid);

						_dayDocumentsViewModel.EditItem(document);
					});

					_incomeExpenseReportViewer.DataBase = _dataBase;
					_incomeExpenseReportViewer.Report = new IncomeExpenseReport(guid =>
						{
							var document = _dataBase.Documents.GetItem(guid);
							_dayDocumentsViewModel.EditItem(document);
						});

					_productReportViewer.DataBase = _dataBase;
					_productReportViewer.Report = new ProductsReport();

					_inflationReportViewer.DataBase = _dataBase;
					_inflationReportViewer.Report = 
						new InflationBySellersReport();
				}
			}
		}

		void OnListDocumentItemDoubleClick(object sender, ListItemEventArgs e)
		{
			_dayDocumentsViewModel.EditItem(e.Item);
		}
		
		private void OnDateTimePickerValueChanged(object sender, EventArgs e)
		{
			if(_dayDocumentsViewModel != null)
				_dayDocumentsViewModel.SelectedDate = _dateTimePicker.Value;
		}

		private void OnMenuItemExitClick(object sender, EventArgs e)
		{
			Close();
		}

		private void OnMenuItemDeleteClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.RemoveItem(_listDocuments.SelectedItem);
		}

		private void OnMenuItemEditClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.EditItem(_listDocuments.SelectedItem);
		}

		private void OnListDocumentsSelectedItemChanged(object sender, EventArgs e)
		{
			_menuItemDelete.Enabled = _listDocuments.SelectedItem != null;
			_menuItemEdit.Enabled = _listDocuments.SelectedItem != null;
		}

		private void OnMenuItemAddDohodClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.Dohod);
		}

		private void OnMenuItemAddRashodClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.Rashod);
		}
		
		private void OnMenuItemAddDohodZaimBackClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.DohodZaimBack);
		}

		private void OnMenuItemAddDohodZaimClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.DohodZaim);
		}

		private void OnMenuItemAddRashodZaimBackClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.RashodZaimBack);
		}

		private void OnMenuItemAddRashodZaimClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.AddDocument(DocumentType.RashodZaim);
		}

		private void OnMenuItemNoteClick(object sender, EventArgs e)
		{
			_dayDocumentsViewModel.NewNote();
		}
	}

	internal class DebtDocumentListItemView : DocumentListItemView
	{

	}

	internal class RepayDebtDocumentListItemView : DocumentListItemView
	{

	}

}